import 'package:flutter/material.dart';
import '../model/searchModel.dart';

class Pages extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              showSearch(context: context, delegate: SearchList());
            },
          ),
        ],
      ),
      body: ExpansionTile(
        title: Text('收起放开'),
        leading: Icon(Icons.ac_unit),
        backgroundColor: Colors.redAccent,
        children: <Widget>[
          Text('32434'),
          Text('32434'),
          Text('32434'),
          Text('32434'),
        ],
      ),
    );
  }
}

class SearchList extends SearchDelegate<String> {
  @override // 清除搜索值
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = ''; // query搜索的值
        },
      ),
    ];
  }

  @override // 关闭按钮
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override // 搜索结果
  Widget buildResults(BuildContext context) {
    return Container(
      width: 100.0,
      height: 100.0,
      child: Card(
        color: Colors.redAccent,
        child: Center(
          child: Text('搜索结果'),
        ),
      ),
    );
  }

  @override // 搜索推荐
  Widget buildSuggestions(BuildContext context) {
    final suggestionList = query.isEmpty
        ? recentSuggests
        : searchModel.where((input) => input.startsWith(query)).toList();
    return ListView.builder(
      itemCount: suggestionList.length,
      itemBuilder: (context, index) => ListTile(
        title: RichText(
          // 富文本
          text: TextSpan(
            // 等同html的span标签
            text: suggestionList[index].substring(0, query.length),
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
            children: [
              TextSpan(
                text: suggestionList[index].substring(query.length),
                style: TextStyle(
                  color: Colors.grey,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
